package com.framework.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import org.apache.commons.lang.StringUtils;

import com.framework.plugin.xmlsql.XmlSqlPlugin;
import com.framework.plugin.xmlsql.tag.ChooseTag;
import com.framework.plugin.xmlsql.tag.DefaultTag;
import com.framework.plugin.xmlsql.tag.ForeachTag;
import com.framework.plugin.xmlsql.tag.IfTag;
import com.framework.plugin.xmlsql.tag.OtherwiseTag;
import com.framework.plugin.xmlsql.tag.SqlTag;
import com.framework.plugin.xmlsql.tag.Tag;
import com.framework.plugin.xmlsql.tag.WhenTag;

@SuppressWarnings({"rawtypes", "unchecked"})
public class SqlUtil {
	
	private static Map<String, Tag> init(Map data) {
		Map<String, Tag> objMap = new HashMap<>();
		objMap.put("if", new IfTag(data));
		ChooseTag ct = new ChooseTag(data);
		WhenTag wt = new WhenTag(data);
		OtherwiseTag ot = new OtherwiseTag(data);
		wt.setChooseTag(ct);
		ot.setChooseTag(ct);
		objMap.put("choose", ct);
		objMap.put("when", wt);
		objMap.put("otherwise", ot);
		objMap.put("foreach", new ForeachTag(data));
		objMap.put("sql", new SqlTag());
		return objMap;
	}
	
	public static String sql(String id, Map data) {
		String sql = "";
		try {
			Map<String, Tag> objMap = init(data);
			String str = XmlSqlPlugin.sqlMap.get(id);
			XMLInputFactory factory = XMLInputFactory.newInstance();  
			InputStream is = new ByteArrayInputStream(str.getBytes());
			XMLEventReader reader = factory.createXMLEventReader(is);  
			String lastTag = null;
			while(reader.hasNext()) {  
			    XMLEvent event = reader.nextEvent();  
			    if(event.isStartElement()) {
			    	StartElement se = event.asStartElement();
			    	String name = se.getName().toString();
	            	lastTag = "start:"+name;
	                DefaultTag tag = (DefaultTag) objMap.get(name);
	                System.out.println(tag.getClass());
	                if(tag!=null) {
	                	tag.setSe(se);
	                	tag.prepare();
	                }
			    } else if(event.isCharacters()) {
			    	String text = event.asCharacters().getData();
	            	if(StringUtils.isBlank(text)) 
	            		continue;
	            	text = " "+text.trim()+" ";
	            	System.err.println(lastTag);
	            	Tag tag = objMap.get(lastTag.replaceFirst("start:", ""));
	            	if(tag!=null) {
		            	text = tag.createSql(text);
		            	System.err.println(text);
		            	sql += text;
	            	}
			    } else if(event.isEndElement()) {
			    	/*String name = event.asEndElement().getName().toString();
	            	lastTag = "end:"+name;
	            	TagEnum te = TagEnum.getEnumByName(name);
	            	if(te!=null) {
		            	Class clazz = te.getClazz();
		            	objMap.put(name, (Tag)(clazz.getConstructor(Map.class).newInstance(data)));
	            	}*/
			    }
			} 
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}  
		return sql;
	}

}
